<template>
	<view class="wallet-list">
		<view class="wallet-list-cell" v-for="(item,index) in dataList" :key="index">
			<view class="wallet-list-cell-top">
				<text class="wallet-list-cell-top-text">{{item.walletName}}</text>
				<view class="wallet-list-cell-top-right-box">
					<view class="wallet-list-cell-top-btn" v-if="item.settleFlag != '1'" @tap="showWithdrawal(item)">
						<text>提现</text>
					</view>
					<text class="wallet-list-cell-top-right-status" v-if="item.settleFlag == '1'">暂不支持</text>
				</view>
			</view>
			<view class="wallet-list-cell-bottom">
				<view class="wallet-list-cell-bottom-item">
					<text>{{item.withdrawalAmount || 0}}</text>
					<text>可提现余额</text>
				</view>
				<view class="wallet-list-cell-bottom-item">
					<text>{{item.pendingRevenueAmount || 0}}</text>
					<text>待入账金额</text>
				</view>
				<view class="wallet-list-cell-bottom-item">
					<text>{{item.frozenAmount || 0}}</text>
					<text>冻结金额</text>
				</view>
			</view>
		</view>
		<xh-alert ref="alertRef" @submit="nextAction"></xh-alert>
	</view>
</template>

<script setup>
	import { onShow } from '@dcloudio/uni-app'
	import { ref } from 'vue'
	import { getWalletsInfo,getSettleList } from '@/common/api/api.js'
	import { useUserStore } from '@/common/stores'
	import { CUSTOMSERVICE_TEL } from '@/common/config/config.js'
	const userStore = useUserStore()
	const dataList = ref([])
	let accountNo = ''
	const alertRef = ref(null)
	// 获取钱包数据
	const getWalletsData = async () => {
		const res = await getWalletsInfo()
		dataList.value = []
		accountNo = res.result.accountNo
		res.result.subAcctVoList.forEach((item)=>{
			if(item.showFlag == '1' || item.incomeBalance > 0) {
				dataList.value.push(item)
			}
		})
	}
	
	// 提现
	const showWithdrawal = async (item) => {
		if(userStore.user.withdrawPwdStatus == '1') {
			//已设置支付密码
			if(item.accountStatus == '1') {
				if(uni.$uv.test.empty(item.settleType)){
					//该账户未开通提现
					uni.$utils.showToast('该账户未开通提现')
					return
				}
				const res = await getSettleList()
				//对公结算卡
				let publicSettleCard = null
				//对私结算卡
				let priviateSettleCards = []
				res.result.forEach(item=>{
					if(item.isPublic == '0'){
						publicSettleCard = item
					}
					else{
						priviateSettleCards.push(item)
					}
				})
				if(userStore.user.agentType == '02') {
					//企业代理商账户
					if(item.settleType == '0' || item.settleType == '2'){
						if(!publicSettleCard){
							alertRef.value.open({
								content:'企业性质代理商，需完成对公结算卡绑定，才可正常提现',
								type:3,
								okText:'立即绑卡'
							})
						}
						else{
							let data = {}
							data.cardList = [publicSettleCard]
							data.wallet = item
							data.accountNo = accountNo
							const dataStr = decodeURIComponent(JSON.stringify(data))
							uni.navigateTo({
								url: `/pages/wallet/wallet-list/withdrawal/withdrawal?data=${dataStr}`
							})
						}
						
					}
					else{
						if(priviateSettleCards.length == 0){
							uni.$utils.showToast('暂无对私结算卡')
							return
						}
						let data = {}
						data.cardList = priviateSettleCards
						data.wallet = item
						data.accountNo = accountNo
						const dataStr = decodeURIComponent(JSON.stringify(data))
						uni.navigateTo({
							url: `/pages/wallet/wallet-list/withdrawal/withdrawal?data=${dataStr}`
						})
					}
				}
				else{
					//个人代理商账户
					if(item.settleType == '0'){
						uni.$utils.showToast('暂不支持对私提现')
						return
					}
					else{
						if(priviateSettleCards.length == 0) {
							uni.$utils.showToast('暂无对私结算卡')
							return
						}
						let data = {}
						data.cardList = priviateSettleCards
						data.wallet = item
						data.accountNo = accountNo
						const dataStr = decodeURIComponent(JSON.stringify(data))
						uni.navigateTo({
							url: `/pages/wallet/wallet-list/withdrawal/withdrawal?data=${dataStr}`
						})
					}
				}
				
			}
			else{
				//已冻结
				alertRef.value.open({
					content:`账户冻结，请联系客服 ${CUSTOMSERVICE_TEL}`,
					type:2,
					okText:'拨打电话'
				})
			}
		}
		else{
			//未设置支付密码
			alertRef.value.open({
				content:'您还未设置支付密码，请先设置支付密码',
				type:1
			})
		}
		
	}
	// 确认操作
	const nextAction = (data)=>{
		console.log(data)
		if(data.type == 1) {
			uni.navigateTo({
				url:'/pages/my/setting/set-payment-password/set-payment-password'
			})
		}
		else if (data.type == 2){
			uni.makePhoneCall({
				phoneNumber:CUSTOMSERVICE_TEL
			})
		}
		else if (data.type == 3){
			uni.navigateTo({
				url:'/pages/wallet/bankcard-list/bankcard-public/bankcard-public'
			})
		}
	}
	onShow(() => {
		getWalletsData()
	})
</script>

<style lang="scss" scoped>
	.wallet-list {
		padding: 30rpx;

		.wallet-list-cell {
			background-color: #fff;
			border-radius: 16rpx;
			padding: 30rpx;
			margin-bottom: 30rpx;

			.wallet-list-cell-top {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.wallet-list-cell-top-text {
					font-size: 28rpx;
					font-weight: bold;
				}

				.wallet-list-cell-top-btn {
					background-color: $xh-theme-color;
					color: #fff;
					height: 40rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 24rpx;
					padding: 0 30rpx;
					border-radius: 20rpx;
				}
				.wallet-list-cell-top-right-status{
					color: $xh-theme-color;
					font-size: 26rpx;
				}
			}

			.wallet-list-cell-bottom {
				display: flex;
				align-items: center;
				justify-content: space-around;
				margin-top: 40rpx;

				.wallet-list-cell-bottom-item {
					display: flex;
					flex-direction: column;
					align-items: center;
					color: $xh-color-9;
					font-size: 26rpx;

					text:nth-child(1) {
						font-size: 40rpx;
						font-weight: bold;
						color: $xh-color-3;
						margin-bottom: 16rpx;
					}
				}
			}
		}
	}
</style>